﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessComponents;
using Tesla.Util.Utilities;
using Tesla.DataAccess.DataAccessLogic.Interfaces;

namespace Tesla.DataAccess.DataAccessLogic.Implementations
{
    public class ClientDA : BaseOperationalDA, IClientDA
    {
        #region BaseOperationalDA Members
        public override int? Create(BaseDC bDC)
        {
            int success = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;

                    success = context.usp_CreateClient(
                                                SetSafeString(((ClientDC)bDC).ClientName),
                                                SetSafeInt(((ClientDC)bDC).ClientParentID),
                                                SetSafeString(((ClientDC)bDC).ClientGeo),
                                                SetSafeInt(((ClientDC)bDC).Client_OUID));


                    context.SubmitChanges();
                    return success;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override int Update(BaseDC bDC)
        {
            int recordCount = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    recordCount = context.usp_UpdateClient(
                                                SetSafeInt(((ClientDC)bDC).ClientID),
                                                SetSafeString(((ClientDC)bDC).ClientName),
                                                SetSafeInt(((ClientDC)bDC).ClientParentID),
                                                SetSafeString(((ClientDC)bDC).ClientGeo),
                                                SetSafeInt(((ClientDC)bDC).Client_OUID));
                    context.SubmitChanges();
                    return recordCount;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> clients = null;
            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_DisplayAllClientResult> clientList = context.usp_DisplayAllClient(gridSize, ref totalRecordCount).Cast<usp_DisplayAllClientResult>();
                    clients = GenericConversion.ConvertToTargetType(clientList.ToList<usp_DisplayAllClientResult>().ToArray(), typeof(usp_DisplayAllClientResult), typeof(ClientDC));

                }

                return clients;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> clients = null;

            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_SearchClientByNameResult> clientList = context.usp_SearchClientByName(name, gridSize, ref totalRecordCount).Cast<usp_SearchClientByNameResult>();
                    clients = GenericConversion.ConvertToTargetType(clientList.ToList<usp_SearchClientByNameResult>().ToArray(), typeof(usp_SearchClientByNameResult), typeof(ClientDC));
                }
                return clients;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadClientResult> clientList = context.usp_ReadClient(id).Cast<usp_ReadClientResult>();
                    return GenericConversion.ConvertToTargetType(clientList.ToList<usp_ReadClientResult>().ToArray(), typeof(usp_ReadClientResult), typeof(ClientDC));
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllClientParents()
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllClientParentsResult> clientList = context.usp_ReadAllClientParents().Cast<usp_ReadAllClientParentsResult>();
                    return GenericConversion.ConvertToTargetType(clientList.ToList<usp_ReadAllClientParentsResult>().ToArray(), typeof(usp_ReadAllClientParentsResult), typeof(ClientParentDC));
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadProjectsForClient(int clientID)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadProjectsForClientResult> projectList = context.usp_ReadProjectsForClient(clientID).Cast<usp_ReadProjectsForClientResult>();
                    return GenericConversion.ConvertToTargetType(projectList.ToList<usp_ReadProjectsForClientResult>().ToArray(), typeof(usp_ReadProjectsForClientResult), typeof(ProjectDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<BaseDC> ReadAllOperatingUnits()
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadAllOperatingUnitsResult> clientList = context.usp_ReadAllOperatingUnits().Cast<usp_ReadAllOperatingUnitsResult>();
                    return GenericConversion.ConvertToTargetType(clientList.ToList<usp_ReadAllOperatingUnitsResult>().ToArray(), typeof(usp_ReadAllOperatingUnitsResult), typeof(OperatingUnitDC));

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadClientByRowNumberRangeResult> clientList = context.usp_ReadEmployeeByRowNumberRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString).Cast<usp_ReadClientByRowNumberRangeResult>();
                    return GenericConversion.ConvertToTargetType(clientList.ToList<usp_ReadClientByRowNumberRangeResult>().ToArray(), typeof(usp_ReadClientByRowNumberRangeResult), typeof(ClientDC));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
